package com.edu.dao.impl;

import com.edu.dao.CartsDao;
import com.edu.entity.OrderDetailEntity;
import com.edu.utils.DatabaseManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 * com.edu.dao.impl
 * 2024/7/11 9:44
 *
 * @author Fancy
 * @version 1.0
 */
public class CartsDaoImpl implements CartsDao {
    private Connection connection;
    public CartsDaoImpl(){
        try {
            this.connection = DatabaseManager.getInstance().getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public int addToCart(OrderDetailEntity cart) {
        try {
            String sql = "INSERT INTO Order_Detail (order_id, book_id, quantity, unit_price) VALUES (?, ?, ?, ?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, cart.getOrderId());
            preparedStatement.setInt(2, cart.getBookId());
            preparedStatement.setInt(3, cart.getQuantity());
            preparedStatement.setDouble(4, cart.getUnitPrice());
            preparedStatement.executeUpdate();
            return cart.getOrderId();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public LinkedList<OrderDetailEntity> getCartItemByOrderId(int orderId) {
        try {
            String sql = "SELECT * FROM Order_Detail WHERE order_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, orderId);
            ResultSet resultSet = preparedStatement.executeQuery();
            LinkedList<OrderDetailEntity> carts = new LinkedList<>();
            while (resultSet.next()) {
                OrderDetailEntity cart = new OrderDetailEntity();
                cart.setOrderId(resultSet.getInt("order_id"));
                cart.setBookId(resultSet.getInt("book_id"));
                cart.setQuantity(resultSet.getInt("quantity"));
                cart.setDetailId(resultSet.getInt("detail_id"));
                cart.setUnitPrice(resultSet.getDouble("unit_price"));
                carts.add(cart);
            }
            return carts;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null; // 如果没有找到，返回null
    }

    @Override
    public int updateCartItemQuantity(int orderId, int cartItemId, int quantity) {
        try {
            String sql = "UPDATE Order_Detail SET quantity = ? WHERE order_id = ? and detail_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, quantity);
            preparedStatement.setInt(2, orderId);
            preparedStatement.setInt(3, cartItemId);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteCartItem(int orderId, int cartItemId) {
        try {
            String sql = "DELETE FROM Order_Detail WHERE order_id = ? and detail_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, orderId);
            preparedStatement.setInt(2, cartItemId);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
